package com.bloody.android;

import com.bloody.android.Bluetooth.BluetoothService;
import com.bloody.android.Observer.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class Turn implements Observer {
	
	private boolean myTurn;
	private int playerToMove;
	private BluetoothService mService;
	private BloodyCrusadeGame bcg;
	
	public Turn(int player, int numberCharacter, BluetoothService service, BloodyCrusadeGame bcg){
		
		if (player==1){
			myTurn = true;
		}
		else{
			myTurn = false;
		}
		this.bcg = bcg;
		mService = service;
		playerToMove = numberCharacter;
		Log.d("DEBUG", ""+numberCharacter);
	}
	
	public void characterDone(){
		playerToMove-=1;
		Log.d("DEBUG", "characterDone");
		if (playerToMove == 0){
			Log.d("DEBUG", "endTurn");
			endTurn();
		}
	}
	
	public void endTurn(){
		myTurn = false;
		Log.d("DEBUG", "showRound");
		bcg.showRoundView(false);
		// mService send message turn for the other player
		mService.write("turn", null);
	}

	public boolean isMyTurn() {
		return myTurn;
	}
	
	public void setMyTurn(boolean myTurn) {
		this.myTurn = myTurn;
	}

	public void startTurn(){
		bcg.allCharactersCanPlay();
		playerToMove = bcg.numberCharacterAlive();
		bcg.showRoundView(true);
	}

	@Override
	public void update(Bundle bundle) {
		if(bundle.getString("type").equalsIgnoreCase("turn"))
			this.startTurn();
	}

}
